package com.xy.hms.service.imp;


import com.xy.hms.mapper.UserMapper;
import com.xy.hms.model.User;
import com.xy.hms.service.UserServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xiaye
 * @create 2020-02-26-16:27
 */
@Service
public class UserServiceImp implements UserServiceI {

    @Autowired
    private UserMapper userMapper;

    /**
     * 登录验证
     * @param toComfirmUser  需要验证的user
     * @return
     * @throws Exception 错误异常
     */
    @Override
    public User userLogin(User toComfirmUser) throws Exception {


        User loginUser = userMapper.selectByUser(toComfirmUser);


        if (loginUser==null) {
            //没查到对应得账号密码
            throw new RuntimeException("账号不存在！");
        }else if((loginUser.getPassword()).equals(toComfirmUser.getPassword())){
            //查成功
            return loginUser;
        }else {
            throw new RuntimeException("密码错误！");
        }


    }

    @Override
    public User shiroLogin(String username) {
        User user = userMapper.selectUserByName(username);
        return user;
    }

    /**
     * 查询对应uid的权限
     * @param uid
     * @return
     */
    @Override
    public List<String> getPermission(Integer uid) {
        List<String> permissions = userMapper.getPermissions(uid);

        return permissions;
    }

    /**
     * 获取登录的角色名称
     * @param toConfirmUser
     * @return
     */
    @Override
    public String getRoleName(User toConfirmUser) {
        String roleName = userMapper.getRoleName(toConfirmUser);
        return roleName;
    }

    @Override
    public String addUser(String username, String password) {
        //返回插入成功的记录数
        int count = userMapper.addUser(username, password);
        if (count!=0){
            return "成功增加"+count+"个用户";
        }else {
            return "增加失败";
        }
    }

    @Override
    public int grantAuthorization() {
        return 0;
    }

    @Override
    public List<User> getUserWithAuth() {
        //带有权限角色的用户
        List<User> userWithPermission = userMapper.selectAllUserWithPermission();

        return userWithPermission;
    }

    //依靠id查询用户和权限
    @Override
    public User getUserWithAuthById(Integer id) {
        User user = userMapper.selectAllUserWithPermissionById(id);
        return user;
    }

    //修改用户权限
    @Override
    public int updateUser(User user) {
        int i = userMapper.updateUser(user);
        return i;
    }

    //更新角色用户表
    @Override
    public int updateUserRole(Integer uid, Integer rid) {

        int i = userMapper.updateUserRole(uid, rid);
        return i;
    }

    //查询用户
    @Override
    public User getUserByCondition(User user) {

        //依靠条件查到的用户
        User user1 = userMapper.selectByUser(user);
        return user1;
    }

    //注册用户时，默认设置普通用户
    @Override
    public int addAuth(Integer uid) {
        int i = userMapper.addUserRole(uid);
        return i;
    }


}
